import cv2
import numpy as np
from pyzbar.pyzbar import decode

cap = cv2.VideoCapture(1)
cap.set(3, 640)
cap.set(4, 480)

while True:
    success, img = cap.read()
    for code in decode(img):
        data = code.data.decode("utf-8")
        pts = np.array(code.polygon, np.int32)
        pts = pts.reshape((-1, 1, 2))
        rect = code.rect
        cv2.polylines(img, [pts], True, (0, 255, 0), 2)
        cv2.putText(img, data, (rect[0], rect[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
    cv2.imshow('Result', img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break
